<?php

class User_IndexController extends Zend_Controller_Action
{
  function init() {
    $this->getHelper('viewRenderer')->setNoRender();
  }

  function getdetailsAction() {
  	
	$id = $this->_getParam('id');
	$email = $this->_getParam('email');
	$pseudo = $this->_getParam('pseudo');
	require_once('Myzen/User.php');
	
	try{
		//empty identifiers
		if(empty($id) && empty($email) && empty($pseudo)) {$user = Zend_Registry::get('app_user');} 
		elseif(!empty($id)) {$user = new Myzen_User(array('id' => $id));}
		elseif(!empty($email)) {$user = new Myzen_User(array('email' => $email));}
		elseif(!empty($pseudo)) {$user = new Myzen_User(array('pseudo' => $pseudo));}
	} catch(Zend_Auth_Exception $e) {
      $data['status'] = 'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data,'serializedphp');	
      return;		
	}
	
	$data['status'] = 'OK';	
	$data['user'] = $user->asArray();	
	echo Zend_Registry::get('dt_serializer')->serialize($data,'serializedphp');
	
  }
  
  
  //Check if a particular id or email is available. 
  //Useful for account creation form to validate online if the email or pseudo is not already reserved
  
  function isavailableAction() {
 	$email = $this->_getParam('email');
	$pseudo = $this->_getParam('pseudo');
	if(empty($email) && empty($pseudo)) {
	  $data['status'] = 'KO';
      $data['error']['num'] = 	'1';
      $data['error']['message'] = 	'Mandatory data is missing';
      echo Zend_Registry::get('dt_serializer')->serialize($data,'serializedphp');	
      return;		
	}

	//we preload the output object with customer not found and after we try to create the customer object
	$data['status'] = 'OK';
	$data['available'] = false;

	try{
		if(!empty($email)) {$user = new Myzen_User(array('email' => $email));}
		elseif(!empty($pseudo)) {$user = new Myzen_User(array('pseudo' => $pseudo));}
	} catch(Zend_Auth_Exception $e) {
		if($e->getMessage() == 'user not found') {
			$data['status'] = 'OK';
			$data['available'] = true;
		} else {
			$data['status'] = 'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
		}
	} //end catch exception Zend_Auth_Exception


    echo Zend_Registry::get('dt_serializer')->serialize($data,'serializedphp');	
	
  } //end function isavailable


} //End class


?>